package com.hbnu.service.impl;

import com.hbnu.pojo.DormBuild;
import com.hbnu.util.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DormBuildDaoImpl extends DormBuildDao {
    @Override
    public List<DormBuild> findAll() {
        // ① 获取连接（数据库地址  用户名 密码）
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;


        try {
            connection = DBUtils.getConnection();
            // ② 准备SQL语句
            String sql = "select * from tb_dormbuild ";
            // ③ 获取集装箱或者说是车
            preparedStatement = connection.prepareStatement(sql);
            // ④执行SQL,获取执行后的结果,查询的结果封装在ResultSet
            rs = preparedStatement.executeQuery();
            // 因为查询出来的结果包括表头信息，所以要指针下移一行，看是否有查询出来的数据
            // 如有数据，就进入循环体，封装该行数据
            List<DormBuild> builds = new ArrayList<>();
            while (rs.next()) {
                DormBuild build = new DormBuild();
                build.setId(rs.getInt("id"));
                build.setName(rs.getString("name"));
                build.setDisabled(rs.getInt("disabled"));
                build.setRemark(rs.getString("remark"));
                builds.add(build);
            }
            return builds;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(rs, preparedStatement, connection);
        }
        return null;
    }
}
